﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JHStock
{
    public partial class FormPictureBox : Form
    {
        private Bitmap bitmap;
        private MovetoCTL.MovetoTracker MT;
        public FormPictureBox(Bitmap bitmap)
        {
            InitializeComponent();
            this.bitmap = bitmap;
            pictureBoxA.Image = this.bitmap;

            _TestR = new Rectangle(); 
            zoombox = new ZoomBox();
        }

        private void SetImage(Bitmap image)
        {
                pictureBoxA.Image = image;
                ReSetPictureBoxImage();
                MT = new MovetoCTL.MovetoTracker(pictureBoxA);
                MT.StartDraw(true);   
        }
        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            PictureBox pb = (PictureBox)sender;
            //if (m_act == Act.ZoomMouse)
                pb.Focus();
        }
        private void pictureBox1_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            PictureBox pb = (PictureBox)sender;
            if (pb.Image == null) return;

            int numberOfTextLinesToMove = e.Delta * SystemInformation.MouseWheelScrollLines / 120;
            double f = 0.0;
            if (numberOfTextLinesToMove > 0)
            {
                for (int i = 0; i < numberOfTextLinesToMove; i++)
                {
                    f += 0.05;
                }
                Zoomrat(f + 1, e.Location);
            }
            else if (numberOfTextLinesToMove < 0)
            {
                for (int i = 0; i > numberOfTextLinesToMove; i--)
                {
                    f -= 0.05;
                }
                Zoomrat(f + 1, e.Location);
            }
        }       
        private void buttonzoomout_Click(object sender, EventArgs e)
        {
            PictureBox pb = (PictureBox)sender;
            if (pb.Image == null) return;
            Point p = new Point(panel1.Width / 2, panel1.Height / 2);
            p.Offset(-pb.Location.X, -pb.Location.Y);
            Zoomrat(1.1F, p);
        }
        private void buttonzoomin_Click(object sender, EventArgs e)
        {
            PictureBox pb = (PictureBox)sender;
            if (pb.Image == null) return;
            Point p = new Point(panel1.Width / 2, panel1.Height / 2);
            p.Offset(-pb.Location.X, -pb.Location.Y);
            Zoomrat(0.9F, p);
        }
        private void Zoomrat(double rat, Point e)
        {
            PictureBox pb = pictureBoxA;
         
            Bitmap bitmap_show = (Bitmap)pb.Image;
            Point L = pb.Location;
            Point S = panel1.AutoScrollPosition;
            int w = (int)(pb.Width * rat);
            int h = w * bitmap_show.Height / bitmap_show.Width;
            L.Offset((int)(e.X * (rat - 1)), (int)(e.Y * (rat - 1)));
            pb.SetBounds(S.X, S.Y, w, h);
            zoombox.UpdateBoxScale(pb);

            S.Offset((int)(e.X * (1 - rat)), (int)(e.Y * (1 - rat)));
            panel1.Invalidate();
            panel1.AutoScrollPosition = new Point(-S.X, -S.Y);
        }
        private void ReSetPictureBoxImage(bool B = false)
        {
            PictureBox pictureBoxn = pictureBoxA;
       
            Bitmap bitmap_show = (Bitmap)pictureBoxn.Image;
            crop_startpoint.X = crop_startpoint.Y = 0;
            int width = pictureBoxn.Height * bitmap_show.Width / bitmap_show.Height;
            int height = pictureBoxn.Width * bitmap_show.Height / bitmap_show.Width;
            if (pictureBoxn.Height > height)
                pictureBoxn.Height = height;
            if (pictureBoxn.Width > width)
                pictureBoxn.Width = width;
            // pictureBox1.Image = bitmap_show;
            zoombox.UpdateBoxScale(pictureBoxn);
            _OriginWith = zoombox.ImageWith(pictureBoxn);
            pictureBoxn.Invalidate();
        }

        public Rectangle _TestR { get; set; }
        private Point crop_startpoint;
        private ZoomBox zoombox;
        private double _OriginWith;
    }
}
